//
//  CBJVisitView.swift
//  Weibo
//
//  Created by 崔健 on 16/5/21.
//  Copyright © 2016年 cbj. All rights reserved.
//

import UIKit
import SnapKit


@objc protocol CBJvisitViewDelegate :NSObjectProtocol{
    
    func loginBtnDidClicked();
    func registerBtnDidClicked();
}


class CBJVisitView: UIView {
    
    
    // 声明代理
    weak var visitDelegate: CBJvisitViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.init(colorLiteralRed: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1)
        
        addSubview(cornerImageView)
        addSubview(maskImageView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(setUpBtn)
        
        cornerImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        iconView.snp_makeConstraints { (make) in
            
            make.centerY.centerX.equalTo(cornerImageView)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            
            make.centerX.centerY.equalTo(iconView)
        }
        
        tipLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(cornerImageView.snp_bottom).offset(40)
            make.centerX.equalTo(cornerImageView)
            make.height.equalTo(40)
            
        }
        
        loginBtn.snp_makeConstraints { (make) in
            
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.left.equalTo(tipLabel)
            make.width.equalTo(80)
        }
        
        setUpBtn.snp_makeConstraints { (make) in
            
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.right.equalTo(tipLabel)
            make.width.equalTo(80)
        }
        
    }
    
    func updateUI(title:String , imageName : String , isHome : Bool = false){
    
        if isHome {
            
            startAnimation()
        }else{
        
            cornerImageView.hidden = true
            iconView.image = UIImage.init(named: imageName)
            tipLabel.text = title
        } 
    }
    
    func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 15;
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        anim.removedOnCompletion = false
        cornerImageView.layer.addAnimation(anim, forKey:nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func loginBtnClick(){
    
        visitDelegate?.loginBtnDidClicked()
    }
    
    func registureBtnClick(){
    
        visitDelegate?.registerBtnDidClicked()
    }
    
    //MARK: 控件懒加载
    private lazy var cornerImageView : UIImageView = {
        let imageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
        
        return imageView
    }()
    
    private lazy var iconView : UIImageView = {
        let imageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_house"))
        
        return imageView
    }()
    
    private lazy var maskImageView : UIImageView = {
    
        let maskImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
        return maskImageView
    }()
    
    private lazy var loginBtn : UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        let image = UIImage.init(named: "common_button_white_disable")
        let w = Int((image?.size.width)! * 0.5)
        let h = Int((image?.size.height)! * 0.5)
        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        
        btn.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
        return btn
    }()
    
    private lazy var setUpBtn : UIButton = {
        
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        let image = UIImage.init(named: "common_button_white_disable")
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
    
        let w = Int((image?.size.width)! * 0.5)
        let h = Int((image?.size.height)! * 0.5)
        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        
        btn.addTarget(self, action: #selector(registureBtnClick), forControlEvents: .TouchUpInside)
        return btn
    }()
    
    private lazy var tipLabel : UILabel = {
    
        let l = UILabel()
        l.text = "关注一些人,回到这里看看有什么惊喜"
        //字号 和颜色
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = .Center
        //设置自动换行
        l.numberOfLines = 0
        
        l.preferredMaxLayoutWidth = 230
        l.contentMode = .Center
        
        return l
    }()
    

}
